package com.autologistics.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.autologistics.model.Goods;
import com.autologistics.model.Media;
import com.autologistics.model.Mediamachine;
import com.autologistics.model.Playlist;

/**
 * @author MikeXiaobu
 *
 */
public class MediaDaoImpl implements MediaDao {
	
	HibernateTemplate hibTemplate;

	public HibernateTemplate getHibTemplate() {
		return hibTemplate;
	}

	public void setHibTemplate(HibernateTemplate hibTemplate) {
		this.hibTemplate = hibTemplate;
	}
	@Override
	public Media getById(int id) {
		// TODO Auto-generated method stub
		String hql="from Media where id=?";
		List<Media> list = hibTemplate.find(hql,id);
		if (!list.isEmpty()){
			return list.get(0);
		}else{
			return null;
		}
	}
	@Override
	public List<Media> listAll() {
		// TODO Auto-generated method stub
		String hql = "from Media";
		List<Media> list = hibTemplate.find(hql);
		return list;
	}
	public List<Mediamachine> queryMediamachineByMediaId(){
		String hql="from Mediamachine";
		List<Mediamachine> listMediamachine=this.hibTemplate.find(hql);
		 System.out.println("fhjgfjhfh");
//		System.out.println("listMediamachine Size"+listMediamachine.size());
		return listMediamachine;
	}
	
	@Override
	public int add(Media media) {		
		// TODO Auto-generated method stub
		System.out.println("add media");
		hibTemplate.save(media);
		List<Mediamachine> listMediamachine=this.queryMediamachineByMediaId();
//		List<Mediamachine> newListMediamachine=new ArrayList<Mediamachine>();
		Mediamachine newMediamachine=new Mediamachine();
		for(Mediamachine m:listMediamachine){
			newMediamachine.setMachine(m.getMachine());
			newMediamachine.setMedia(media);
//			newListMediamachine.add(newMediamachine);
			this.hibTemplate.save(newMediamachine);
		}	
		return 0;
	}

	@Override
	public int delete(Media media) {
		// TODO Auto-generated method stub
		hibTemplate.delete(media);
		return 0;
	}

	@Override
	public Media get(Media media) {
		// TODO Auto-generated method stub
		
		Media m = hibTemplate.get(Media.class, media.getId());
		return m;

	}
	
	public int allRow(){
		String hql="from Media order by id";
		int allRow =this.hibTemplate.find(hql).size();
		return allRow;
	}
	
	/**
	 * 得到某一页的数据
	 * @param offset
	 * @param length
	 * @param userId
	 * @return
	 */
	public List<Media> queryForOnePage(final int offset, final int length){
		System.out.println("MediaDaoimpl:  queryForOnePage");
		String hql="from Media order by id";
		Session session=hibTemplate.getSessionFactory().openSession();
//		Session session =HibernateSessionFactory.getSession();
	    Query q = session.createQuery(hql);  
	    q.setFirstResult(offset);  
	    q.setMaxResults(length);  
	    List<Media> mediaList = q.list();  
	  //  System.out.println(goodsList.size());  
	    session.close();  
	    return mediaList;  
	}
	
	//to get media through special id.
	public Media queryMediaById(int mediaId){
		String hql="from Media where id=?";
		List<Media> mediaList=this.hibTemplate.find(hql, mediaId);
		Media media=mediaList.get(0);
		if(media!=null){
			return media;
		}else{
			return null;
		}
	}
	
	//删除指定的商品
	public void deleteMedia(int mediaId){
		//因为有外码约束，所以先进行mediamachine中的删除
	//	this.hibTemplate.delete();
		this.hibTemplate.delete(this.queryMediaById(mediaId));
	}
	
	public void updateMedia(Media media){
			
		this.hibTemplate.update(media);
	}
	
	public List<Media> searchMedia(String filename){
		try{
			String hql="from Media where filename like '%"+filename+"%'";
			List<Media> mediaList=this.hibTemplate.find(hql);
			return mediaList;	
		}catch(RuntimeException e){
			throw e;
		}
	}
	
	public void addPlayList(Playlist playlist){
		hibTemplate.save(playlist);
	}

}
